package com.uwosh.cs342.g6.mapapp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.os.AsyncTask;
import android.util.Log;

public class DatabaseHandler extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = "MapApp.db";
	private static final int DATABASE_VERSION = 1;

	private static int current_user_id;
	private static String current_user_username;
	private static String current_user_password;

	public DatabaseHandler(Context context) {
		// Log.d("DBC", "Database Constructor");
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	public void insertUser(User user) {
		SQLiteDatabase db = this.getWritableDatabase();
		current_user_id = user.id;
		current_user_username = user.username;
		current_user_password = user.password;

		String DELETE = "DELETE FROM user";

		String INSERT_USER = "INSERT INTO user (Id,Username,Password) values ("
				+ "'" + current_user_id + "'" + ", " + "'"
				+ current_user_username + "'" + ", " + "'"
				+ current_user_password + "'" + ");";

		db.execSQL(DELETE);

		db.execSQL(INSERT_USER);
		db.close();
	}

	@Override
	public void onCreate(SQLiteDatabase db) {



		String CREATE_LOCA_USER_TABLE = "CREATE TABLE user ("
				+ "Id INTEGER PRIMARY KEY ," + "Username TEXT,"
				+ " Password TEXT)";

		db.execSQL(CREATE_LOCA_USER_TABLE);

	}

	public int getUserNum() {
		SQLiteDatabase db = this.getWritableDatabase();

		String SQL = "SELECT * FROM USER";

		int toReturn = -1;

		Cursor cursor = db.rawQuery(SQL, null);

		if (cursor.moveToFirst()) {

			toReturn = Integer.parseInt(cursor.getString(0));

		}
		cursor.close();

		db.close();

		// return contact list
		return toReturn;
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
	}

	public void logoutCurrentUser() {
		SQLiteDatabase db = this.getWritableDatabase();

		int userId = -1;

		Login x = new Login();
		userId = x.currentUserId;
		String sql = "DELETE FROM user WHERE Id =" + userId;

		db.execSQL(sql);

		db.close();

	}


	








	

}